package architecture.dbinterface;

import java.sql.*;
import java.util.*;

import db.*;

public class FullDatabase extends DatabaseInterface {

    private String[] groups = new String[]{"administrator","seller", "buyer", "guest"};
    private HashMap<String, String> userPasswords = new HashMap<String, String>();
       
    private Random rand = new Random();
   
   
    private ebDB database;
    private ebDBDataSet tables;
   
    public FullDatabase()
    {
        database  = ebDB.getInstance();
        System.out.println(database.Connect());
    }
   
    @Override
    public String[] getAllTables() {
       
        DatabaseMetaData metaData = database.getDatabaseMetaData();
        Vector<String> result = new Vector<String>();
        try {
            ResultSet tables = metaData.getTables(database.getDatabaseName (), "dbo", "%", null);
           
            while (tables.next()) {
                result.add(tables.getString(3));
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return result.toArray(new String[result.size()]);
    }

    public Vector<String> getRestrictionsForUser(String user) {
        return new Vector<String>();
    }
   
    public Vector<String> getRightsForGroup(String group)
    {
        Vector<String> pravaGrupe = new Vector<String>();
                    
        try
        {
            ebDBDataSet ds = new ebDBDataSet();
            ds.setTableName("KORISNICKE_GRUPE");
            ds.Prepare();
            ds.Open();
           
            ResultSet rs = ds.getResultSet();
        	
            String sg = "";
            System.out.println(group);
           
            while(rs.next())
            {
                if (rs.getString("NAZIV_GRUPE").compareTo(group)==0)
                    {
                        sg = rs.getString("SIFRA_GRUPE");
                        break;
                    }
            }
           System.out.println(sg);
            rs.close();
           
            ds = new ebDBDataSet();
            ds.setTableName("IMA_PRAVO");
            ds.Prepare();
            ds.Open();
            rs = ds.getResultSet();
            Vector<String> sifrePrava = new Vector<String>();
           
            while(rs.next())
            {
                if ( rs.getString("SIFRA_GRUPE").compareTo(sg)==0)
                    sifrePrava.add(rs.getString("SIFRA_PRAVA"));               
            }
           System.out.println(sifrePrava);
            ds = new ebDBDataSet();
            ds.setTableName("KORISNICKA_PRAVA");
            ds.Prepare();
            ds.Open();   
            rs = ds.getResultSet();
           
            while(rs.next())
            {
                if (sifrePrava.contains (rs.getString("SIFRA_PRAVA")))
                    pravaGrupe.add(rs.getString("NAZIV_PRAVA"));                   
            }    
            System.out.println(pravaGrupe);
         } catch (SQLException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
         return pravaGrupe;
    }
       
    // vraca ime grupe
    @Override
    public String getGroupForUser(String username)
    {       
    	if (username.equals("guest")) return "guest";
        try
        {
        	System.out.println(username);
            ebDBDataSet ds = new ebDBDataSet();
            ds.setTableName("KLIJENTI_KORISNICI");
            ds.Prepare();
            ds.Open();
           
            ResultSet rs = ds.getResultSet();
           
            String sg = "";

        	
            while(rs.next())
            {
                if (rs.getString("KL_IME").compareTo(username)==0) {
                    sg = rs.getString("SIFRA_GRUPE");
                    break;
                }
            }
           
            ds.Close();
           
            ds = new ebDBDataSet();
            ds.setTableName("KORISNICKE_GRUPE");
            ds.Prepare();
            ds.Open();
            rs = ds.getResultSet();
            
            while(rs.next())
            {
                if (rs.getString("SIFRA_GRUPE").compareTo(sg)==0)
                    return rs.getString("NAZIV_GRUPE");
            }
        }
        catch (NumberFormatException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        catch (SQLException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
       
        return "";
    }

    @Override
    public String getPassForUser(String username)
    {
        ebDBDataSet ds = new ebDBDataSet();
        ds.setTableName("KLIJENTI_KORISNICI");
        ds.Prepare();
        ds.Open();
       
        ResultSet rs = ds.getResultSet();
       
        try
        {
            while(rs.next())
            {
                if (rs.getString("KL_IME").compareTo(username)==0)
                    return rs.getString("KL_LOZINKA");
            }
        }
        catch (SQLException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
       
        return "";
    }

	@Override
	public void disconnect() {
		database.Disconnect();
		
	}

}